package com.shujia.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class MakeDataStudent {

    public static void main(String[] args) throws Exception {

        /**
         * 模拟生成数据保存到MySQL中
         *
         */
        FileReader fileReader = new FileReader("spring/data/students.txt");

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line;

        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            int age = Integer.parseInt(split[2]);
            String gender = split[3];
            String clazz = split[4];

            //将数据保存到数据库
            Connection con = JDBCUtil.getCon();

            //插入数据
            PreparedStatement ps = con.prepareStatement("insert into student(id,name,age,gender,clazz) values(?,?,?,?,?)");

            for (int i = 0; i < 1000; i++) {

                String newId = id + i;

                //设置参数
                ps.setString(1, newId);
                ps.setString(2,name);
                ps.setInt(3,age);
                ps.setString(4,gender);
                ps.setString(5,clazz);

                //增加一行
                ps.addBatch();
            }

            //每次插入多条数据
            ps.executeBatch();

            //关闭连接
            con.close();

            System.out.println("1000条保存成功");
        }
    }
}
